﻿
//using HiQPdf;
//using iText.Html2pdf;
//using iText.Html2pdf.Resolver.Font;
//using iText.Kernel.Font;
//using iText.Kernel.Pdf;
//using iText.StyledXmlParser.Css.Media;
//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Text;

//namespace XW.Utils
//{
//    /// <summary>
//    /// PDF 转换  帮助
//    /// </summary>
//    public static partial class CommonHelper
//    {
//        /// <summary>
//        /// HTML 转PDF
//        /// </summary>
//        /// <param name="htmlString">The HTML string.</param>
//        /// <returns></returns>
//        public static string HTML2PDF(string htmlString, string filePath, int orientation = 0, int pageSize = 8) {
//            var options = new ConverterProperties();
//            options.SetMediaDeviceDescription(new iText.StyledXmlParser.Css.Media.MediaDeviceDescription(MediaType.PRINT));
//            var writer = new PdfWriter(filePath + ".pdf");
//            PdfDocument pdf = new PdfDocument(writer);
//            //pdf.AddFont(PdfFontFactory.CreateFont("STSongStd-Light", "UniGB-UCS2-H", false)); 
//            iText.Kernel.Geom.PageSize ps = iText.Kernel.Geom.PageSize.A4;
         
//            if (orientation == 0)
//            {
//                ps = iText.Kernel.Geom.PageSize.A4.Rotate();
//            }
//            pdf.SetDefaultPageSize(ps);
//            htmlString = "<meta charset=\"UTF-8\">" + htmlString;
//            options.SetFontProvider(new DefaultFontProvider(true,true,true));
//            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
//            mediaDeviceDescription.SetWidth(ps.GetWidth());
//            options.SetMediaDeviceDescription(mediaDeviceDescription);
//            HtmlConverter.ConvertToPdf(htmlString, pdf, options);
//            //// create the HTML to PDF converter
//            //HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
//            //// set browser width
//            //// htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
//            //// set browser height if specified, otherwise use the default
//            ////if (textBoxBrowserHeight.Text.Length > 0)
//            ////    htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);
//            //// set PDF page size and orientation
//            //htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
//            //htmlToPdfConverter.Document.PageOrientation = orientation == 0? PdfPageOrientation.Landscape: PdfPageOrientation.Portrait;
//            //htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

//            //// set the PDF standard used by the document 
//            ////htmlToPdfConverter.Document.PdfStandard =   PdfStandard.Pdf;

//            //// set PDF page margins
//            //htmlToPdfConverter.Document.Margins = new PdfMargins(10);

//            ////htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;

//            //htmlToPdfConverter.ConvertHtmlToFile(htmlString, "", filePath+".pdf");

//            //PdfDocument pdf = new PdfDocument(new PdfReader(filePath + ".pdf"), new PdfWriter(filePath + "d.pdf"));
//            //var numberOfPages = pdf.GetNumberOfPages();

//            //for (var i = 1; i <= numberOfPages; i++)
//            //{
//            //    // PdfAnnotation 
//            //    PdfDictionary pageDict = pdf.GetPage(i).GetPdfObject();
//            //    PdfArray texts = pageDict.GetAsArray(PdfName.Text);
//            //    for (int j = 0; j < texts.Size(); j++)
//            //    {
//            //        PdfDictionary annotation = texts.GetAsDictionary(j);
//            //        if (PdfName.Watermark.Equals(annotation.GetAsName(PdfName.Subtype)))
//            //        {
//            //            annotation.Clear();
//            //        }
//            //    }
//            //}
//            ////var layers = pdf.GetCatalog().GetOCProperties(true).GetLayers();
//            ////foreach (var lay in layers)
//            ////{
//            ////    var t = lay.GetPdfObject().Get(PdfName.Name);

//            ////    if (t.ToString().Equals("watermark"))
//            ////    {
//            ////        //Not what I want..need to remove the layer
//            ////        lay.SetOn(false);
//            ////        //This does not work...
//            ////        //layers.RemoveAt(i);
//            ////    }
//            ////}
//            //pdf.Close();
//            return filePath + ".pdf";
//        }
//        /// <summary>
//        /// HTML 转PDF
//        /// </summary>
//        /// <param name="htmlString">The HTML string.</param>
//        /// <returns></returns>
//        public static byte[] HTML2PDF2(string htmlString, int orientation = 0, int pageSize = 8)
//        {
//            // create the HTML to PDF converter
//            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
//            // set browser width
//            // htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);
//            // set browser height if specified, otherwise use the default
//            //if (textBoxBrowserHeight.Text.Length > 0)
//            //    htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);
//            // set PDF page size and orientation
//            htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
//            htmlToPdfConverter.Document.PageOrientation = orientation == 0 ? PdfPageOrientation.Landscape : PdfPageOrientation.Portrait;


//            // set the PDF standard used by the document 
//            //htmlToPdfConverter.Document.PdfStandard =   PdfStandard.Pdf;

//            // set PDF page margins
//            htmlToPdfConverter.Document.Margins = new PdfMargins(10);

//            //htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
//            return htmlToPdfConverter.ConvertHtmlToMemory(htmlString, ""); 
//        }
//    }
//}
